package game.unitmanager 
{
	import game.map.Army;
	import game.map.Province;
	import game.map.Unit;
	import game.map.UnitTypes;
	import game.mapviewer.ArmyIcon;
	import game.unitmanager.commands.MoveCommand;
	/**
	 * ...
	 * @author rnk
	 */
	public class ArmyController 
	{
		public var model:Army;
		public var view:ArmyIcon;
		public var papa:UnitsManager;
		
		//ugly but ok
		public var loyalty:int = 0;
		public var order:int = -1;
		public var supportProvince:String;
		
		public function ArmyController() 
		{
			
		}
		
		public function AddUnit(unitType:String):void
		{
			var unitModel:Unit = new Unit(unitType);
			model.AddUnit(unitModel);
			view.Refresh();
		}
		
		public function RemoveUnit(unit:Unit):void
		{
			model.RemoveUnit(unit);
			view.Refresh();
		}
		
		public function SetOrder(order:int=-1):void
		{
			this.order = order;
			view.ShowOrder(order);
		}
		
		public function MoveTo(destinationProvinceUid:String,callback:Function):void
		{
			var moveCommand:MoveCommand = new MoveCommand(this, destinationProvinceUid);
			moveCommand.callback = callback;
			moveCommand.Execute();
			
		}
		
		public function GetInitiative():Number
		{
			return model.units.length;
		}
		
		public function GetPower(isAttacking:Boolean=true):int
		{
			var result:int = 0;
			for each (var unit:Unit in model.units) 
			{
				var paramName:String = isAttacking?"attack":"defend";
				result += UnitTypes.UNIT_PARAMS[unit.type][paramName];
			}
			return result;
		}
		
		public function Kill():void
		{
			view.papa.RemoveArmy(view);
			if (model.province) model.province.RemoveArmy();
			papa.RemoveArmy(this);
		}
		
		
	}

}